Socket
Socket
Sign inDemoInstall

throwable

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

throwable

Inherit from Error without performance penalties, instanceof behaves as expected and the error stack is correct


Version published
Weekly downloads
1
decreased by-85.71%
Maintainers
1
Weekly downloads
 
Created
Source

node-throwable

Inherit from Error without performance penalties, instanceof behaves as expected and the error stack is correct. This library can be used cross-browser using Browserify.

Usage

A lot of solutions for inheriting from Error in javascript are error-prone (pun intended) and tricky.

Using this library you can inherit Error just like you would inherit from any other javascript object/class. Here is an example using the inherits library.

var Throwable = require('throwable');

function MyError(wrapped)
{
        Throwable.call(this, wrapped);
        this.name = 'MyError';
}

require('inherits')(MyError, Throwable);

And this is how you can throw your error:

function failure()
{
        throw new MyError(Error('Something went wrong!'));
}

Advantages

This library has a few advantage some other solutions might not have:

Using instanceof wil work as expected:

try
{
        failure();
}
catch(err)
{
        console.log(err instanceof Error); // true
        console.log(err instanceof Throwable); // true
        console.log(err instanceof MyError); // true
}

And the stack, fileName (firefox), lineNumber (firefox) attributes will be set correctly (instead of pointing to a line in the Throwable constructor).

Also, the stack attribute is accessed only when you need it, Error.captureStackTrace is not used either. Accessing any of these two is a big performance hit in Node.js and Chrome.

Keywords

FAQs

Package last updated on 06 Mar 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc